
#include"process_kill.h"

int wait_mark;

void killing()
{
	int p1,p2,l0;
	while((p1 = fork()) == -1)
    {
        ;
    }
	if(p1 > 0)
	{
		while((p2 = fork()) == -1)
        {
            ;
        }
		if(p2 > 0)
		{
			wait_mark = 1;
			signal(SIGINT, stoping);
			waiting();
			kill(p1, 16);
			kill(p2, 17);
			wait(0);
			wait(0);
			printf("Parent process is killed!\n");
			exit(0);
		}
		else
		{
			wait_mark = 1;
			signal(17, stoping);
			waiting();
			lockf(l0, 1, 0);
			printf("Child process 2 is killed by parent!\n");
			lockf(l0, 0, 0);
			exit(0);
		}
	}
	else
	{
		wait_mark = 1;
		signal(16, stoping);
		waiting();
		lockf(l0, 1, 0);
		printf("Child process 1 is killed by parent\n");
		lockf(l0, 0, 0);
		exit(0);
	}
}

void waiting()
{
	while(wait_mark != 0)
    {
        ;
    }
}

void stoping()
{
	wait_mark = 0;
}
